import { Context } from 'koa';
import { login, register } from '../services/auth.service';
import { formatResponse } from '../utils/formatUtil';
import { UserLoginDto, UserRegisterDto } from '../dtos/auth.dto';

class AuthController {
  /**
   * 登录控制器
   * @param {Context} ctx - Koa 上下文
   */
  async loginController(ctx: Context) {
    const user = ctx.request.body as UserLoginDto;
    const result = await login(user);
    ctx.status = 200;
    ctx.body = formatResponse(result, '登录成功', 200);
  }

  /**
   * 注册控制器
   * @param {Context} ctx - Koa 上下文
   */
  async registerController(ctx: Context) {
    const user = ctx.request.body as UserRegisterDto;
    const newUser = await register(user);
    ctx.status = 201;
    ctx.body = formatResponse(newUser, '注册成功', 201);
  }
}

export const authController = new AuthController();
